import { defineStore } from "pinia";
import persistedstateConfig from "@/store/config/index";
import { getUserInfoAPI } from "@/api/modules/user/index";

interface Account {
  user: any; // 用户信息
  roles: string[]; // 角色
  permissions: string[]; // 权限
}

/**
 * 用户信息
 * @methods setAccount 设置账号信息
 * @methods setToken 设置token
 * @methods logOut 退出登录
 */
const userInfoStore = () => {
  // 账号信息
  const account = ref<Account>({
    user: {}, // 用户信息
    roles: [], // 角色
    permissions: [] // 权限
  });
  // token
  const token = ref<string>("");

  // 设置账号信息
  async function setAccount() {
    let obj = {
      "user": {
        "id": 1,
        "deptId": "100",
        "deptName": "研发部门",
        "userName": "admin",
        "nickName": "超级管理员",
        "email": "2547096351@qq.com",
        "phone": "15888888888",
        "sex": 1,
        "avatar": "https://ooo.0x0.ooo/2025/04/10/O0dG7r.jpg",
        "status": 1,
        "description": "系统初始用户",
        "roles": [
          {
            "id": 1,
            "name": "超级管理员",
            "code": "admin",
            "sort": 1,
            "status": 1,
            "admin": true,
            "description": "默认角色，超级管理员，上帝角色",
            "createBy": null,
            "createTime": "2024-03-27 11:21:01",
            "updateBy": null,
            "updateTime": null
          }
        ],
        "loginIp": "0:0:0:0:0:0:0:1",
        "loginDate": "2025-03-31 10:30:59",
        "createBy": "admin",
        "createTime": "2024-03-19 11:21:01",
        "updateBy": null,
        "updateTime": null,
        "admin": true
      },
      "roles": [
        "admin"
      ],
      "permissions": [
        "*:*:*"
      ]
    }
    // 存储用户信息
    // let data = await getUserInfoAPI();
    account.value = obj;
  }
  // 设置token
  async function setToken(data: string) {
    token.value = data;
  }
  // 退出登录
  async function logOut() {
    // 清除账号数据
    account.value = {
      user: {},
      roles: [],
      permissions: []
    };
    token.value = "";
  }

  return { account, token, setAccount, setToken, logOut };
};

export const useUserInfoStore = defineStore("user-info", userInfoStore, {
  persist: persistedstateConfig("user-info", ["token"])
});
